package utils;

import java.awt.geom.Point2D;

import ctf.network.ProtocolError;

/**
 *  Parser to encode and decode string for various points
 * @author N. Bhatia, A.Butt, N, Evans, C. Levesque, P. OKeefe
 *
 */
public class Point2D_DataPackage implements IDataPackage {
	public Point2D point;
	
	@Override
	public void decode(String encodedData) throws ProtocolError {
		point = new Point2D.Float();
		String pointString = encodedData.substring(1, encodedData.length() - 1);
		String[] twoValues = pointString.split(",");
		if (twoValues.length == 2) {
				point.setLocation(
					Float.parseFloat(twoValues[0]),
					Float.parseFloat(twoValues[1]));
		}else{
			throw new ProtocolError("(Float, Float)", encodedData);
		}
	}

	@Override
	public String encode() {
		String encoded = new String("("+(float)point.getX()+","+(float)point.getY()+")");
		return encoded;
	}

}
